package com.server.push.ws.base;

import com.alibaba.fastjson2.JSON;
import lombok.Data;

import javax.websocket.Session;
import java.util.HashSet;
import java.util.Set;

@Data
public class WsClient {
    public static Set<WsClient> wsClients = new HashSet<>();

    private Session session;


    private WsClient(Session session){
        this.session = session;
    }

    public static WsClient initialization(Session session){
        for (WsClient wsClient : wsClients) {
            if (wsClient.getSession().equals(session)){
                return wsClient;
            }
        }
        return new WsClient(session);
    }

    public boolean isOpen(){
        return session.isOpen();
    }

    public void send(Object o){
        if (session.isOpen())session.getAsyncRemote().sendText(JSON.toJSONString(o));
    }


}
